﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Mail;
using System.IO;

namespace EnviarMail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnEnviar_Click(object sender, EventArgs e)
        {
            MailMessage mail = new MailMessage();

            mail.To.Add(txtMailPara.Text);
            mail.Subject = txtMailAsunto.Text;

            MailAddress mailfrom = new MailAddress(txtUserNameSend.Text);
            
            mail.From = mailfrom;

           
            mail.Body = mailBody.Text;
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            
            SmtpClient smtp;
            
            smtp = new SmtpClient(txtServidor.Text);
            
            smtp.UseDefaultCredentials = true;
            smtp.Credentials = new NetworkCredential(txtUserNameSend.Text,txtPassword.Text);
            smtp.EnableSsl = cbUseSSL.Checked;
         
            try
            {
                smtp.Send(mail);
                MessageBox.Show("Correo enviado correctamente. Servidor:" + txtServidor.Text);
                //Console.WriteLine("Servidor que funciono: ");
            }
            catch (Exception ex)
            {
               MessageBox.Show(ex.Message + "\n" + ex.InnerException);
            }
         
        }

        private List<string> obtenerListaServidoresSMTP()
        {
            List<string> lista = new List<string>();
            StreamReader arch = null;
            try
            {
                arch = new StreamReader("C:\\Documents and Settings\\Administrador\\Mis documentos\\Visual Studio 2008\\EnviarMail\\ListaSMTPServer.txt");
                string linea = "";
                while (linea != null)
                {
                    linea = arch.ReadLine();
                    if (linea != null)
                        lista.Add(linea);
                }
                arch.Close();
                return lista;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                if (arch != null)
                    arch.Close();
                return lista;
            }

        }

    }
}
